import request from "@/utils/request";
import type {
  GetPermissionListResponse,
  PermissionItem,
} from "./models/permissionModels";

enum Api {
  getPermissionListUrl = `/admin/acl/permission`,
  addPermissionUrl = `/admin/acl/permission/save`,
  updatePermissionUrl = `/admin/acl/permission/update`,
  removePermissionUrl = `/admin/acl/permission/remove`,
  getRolePermissionListUrl = `/admin/acl/permission/toAssign`,
  assignRolePermissionListUrl = `/admin/acl/permission/doAssignAcl`,
}

/**
 * 获取菜单列表
 * @returns GetPermissionListResponse
 */
export const getPermissionListApi = () => {
  return request.get<any, GetPermissionListResponse>(Api.getPermissionListUrl);
};

/**
 * 添加菜单
 * @param permission 菜单
 * @returns null
 */
export const addPermissionApi = (permission: PermissionItem) => {
  return request.post<any, null>(Api.addPermissionUrl, permission);
};

/**
 * 更新菜单
 * @param permission 菜单
 * @returns null
 */
export const updatePermissionApi = (permission: PermissionItem) => {
  return request.put<any, null>(Api.updatePermissionUrl, permission);
};

/**
 * 删除菜单
 * @param id 菜单id
 * @returns null
 */
export const removePermissionApi = (id: string) => {
  return request.delete<any, null>(Api.removePermissionUrl + `/${id}`);
};

/**
 * 获取角色的菜单列表
 * @param roleId 角色id
 * @returns GetPermissionListResponse
 */
export const getRolePermissionListApi = (roleId: string) => {
  return request.get<any, GetPermissionListResponse>(
    Api.getRolePermissionListUrl + `/${roleId}`
  );
};

/**
 * 给角色分配权限列表
 * @param roleId 角色id
 * @param permissionIdList 权限列表
 * @returns null
 */
export const assignRolePermissionListApi = (
  roleId: string,
  permissionIdList: string[]
) => {
  return request.post<any, null>(Api.assignRolePermissionListUrl, {
    roleId,
    permissionIdList,
  });
};
